enum REGISTER_TYPE {
  NORMAL = "NORMAL", // 日常注册
  TARGET_SALES = "TARGET_SALES", // TS白名单注册
}

interface AuthOptions {
  agree : boolean;
  location : string;
  registerType : REGISTER_TYPE;
}

interface AuthGateOptions {
  trackParams : object;
  location : string;
  registerType : REGISTER_TYPE;
}

interface AgreeOptions {
  btnName : string;
  registerType : REGISTER_TYPE;
  popupName: string
}

interface RegisterOptions {
  success : boolean;
  successType ?: "成功入会" | "成功绑定";
  failReason ?: string;
}

class RegisterTracker {
  private exec = true;
  private log = true;
  private consoleLog(
    desc : string,
    eventId : string,
    eventData : Record<string, any> = {}
  ) {
    if (this.log) {
      // console.log(`RegisterTracker => ${desc}:`, {
      //   eventId,
      //   eventData,
      // });
    }
  }

  private track(
    eventId : string,
    eventData : Record<string, any> = {},
    desc = "埋点触发"
  ) {
    this.consoleLog(desc, eventId, eventData);
    if (!this.exec) {
      return;
    }
    // @ts-ignore
    getApp().commonClTrack(eventId, eventData);
  }

  private transformLocaiton(key : string) : string {
    if (!key) {
      return "";
    }
    const LOCATION_MAPPING : Record<string, string> = {};

    return LOCATION_MAPPING[key] || key;
  }

  /** ---------- 下面开始是事件 ---------- */


  /**
   * 小程序-授权手机号
   */
  authMobile(opt : AuthOptions) {
    const { agree, location, registerType } = opt;
    this.track(
      "c_ebmp_auth_mobile",
      {
        c_answer: agree ? "允许" : "拒绝",
        c_location: this.transformLocaiton(location),
        c_register_type:
          registerType === REGISTER_TYPE.NORMAL ? "日常注册" : "TS白名单注册",
      },
      "小程序-授权手机号"
    );
  }

  /**
   * 小程序-同意个保法及年满18周岁
   * 用户在个保信息及年满18周岁确认弹窗中，点击“提交”或TS白名单注册流程中新会员注册弹窗中“立即绑定”，触发该事件新增注册类型
   */

  authGate(opt : AuthGateOptions) {
    const { trackParams, registerType, location } = opt;
    // {
    //   c_age_answer: argeeAge ? "允许" : "拒绝",
    //   c_pii_answer: argeePii ? "允许" : "拒绝",
    //   c_location: this.transformLocaiton(location),
    //   c_register_type:
    //     registerType === REGISTER_TYPE.NORMAL ? "日常注册" : "TS白名单注册",
    // }
    let params = {
      c_location: this.transformLocaiton(location),
      c_register_type: registerType === REGISTER_TYPE.NORMAL ? "日常注册" : "TS白名单注册",
    }
    let trackParamsAll = Object.assign({}, trackParams, params)
    this.track(
      "c_ebmp_auth_gate",
      trackParamsAll,
      "小程序-同意个保法及年满18周岁"
    );
  }

  /**
   * 小程序-同意TMS会员规则
   */
  authViprules(opt : AgreeOptions) {
    const { btnName, registerType } = opt;
    this.track(
      "c_ebmp_auth_viprules",
      {
        c_answer: btnName, // 立即加入/稍后加入/立即注册/重新授权
        c_register_type:
          registerType === REGISTER_TYPE.NORMAL ? "日常注册" : "TS白名单注册",
      },
      "小程序-同意TMS会员规则"
    );
    getApp().commonssPopupTrack('PopupClick', {'popup_type' : registerType === REGISTER_TYPE.NORMAL ? '会员注册/绑定' : 'TS活动', 'popup_name': registerType === REGISTER_TYPE.NORMAL ? '入会弹窗' : 'TS入会弹窗', 'button_name': btnName})
    // this.$commonSensors.popupEvent('PopupClick', )
  }

  /**
   * 小程序-同意个人信息完善
   */
  authPiigate(opt : AgreeOptions) {
    const { btnName, registerType, popupName } = opt;
    this.track(
      "c_ebmp_auth_piigate",
      {
        c_answer: btnName, // 立即完善/稍后完善
        c_register_type:
          registerType === REGISTER_TYPE.NORMAL ? "日常注册" : "TS白名单注册",
      },
      "小程序-同意个人信息完善"
    );
    getApp().commonssPopupTrack('PopupClick', {'popup_type' : registerType === REGISTER_TYPE.NORMAL ? '会员注册/绑定' : 'TS活动', 'popup_name': popupName, 'button_name': btnName})
  }

  /**
   * 小程序-授权手机号入会/绑定结果
   * 点击立即加入，授权手机号后返回绑定/入会结果
   */
  joinVipResult(options : RegisterOptions) {
    const { success, successType, failReason } = options;
    this.track(
      "c_ebmp_join_vip_result",
      {
        c_success: success ? "是" : "否",
        c_success_type: successType || "",
        c_fail_reason: failReason || "",
      },
      "小程序-授权手机号入会/绑定结果"
    );
  }
  /**
   * 小程序-点击立即绑定会员
   * 点击入会成功弹窗上 我已注册会员立即绑定
   */
  bindVip() {
    this.track("c_ebmp_bind_vip", {}, "程序-点击立即绑定会员");
  }

  /**
   * 首页-特邀会场-继续绑定/继续加入
   * 点击特邀会场弹窗上的“继续绑定”或“继续加入”按钮后返回
   */
  targetSalesContinueToJoinVip(options : RegisterOptions) {
    const { success, successType, failReason } = options;
    this.track(
      "c_ebmp_target_sales_continue_to_join_vip",
      {
        c_success: success ? "是" : "否",
        c_success_type: successType || "",
        c_fail_reason: failReason || "",
      },
      "首页-特邀会场-继续绑定/继续加入"
    );
  }

  /**
   * 首页-特邀会场-进入会场失败
   * 出现 非受邀人群/活动未开始/活动已结束弹窗时
   * 由于 <register-widget /> 会被多个页面引用, init逻辑也基本会走, ts活动会先进 special 页面, 若活动有问题, 则导航至home也, 会触发两次, 需要做个判断
   * 以后ts触发页修改, 埋码也需要同步修改
   */
  targetSalesFailToParticipate(failReason : string, location : string) {
    if (location !== "special") {
      return;
    }
    this.track(
      "c_ebmp_target_sales_fail_to_participate",
      {
        c_fail_reason: failReason, // 非受邀人群/活动未开始/活动已结束
      },
      "首页-特邀会场-进入会场失败"
    );
  }

  /**
   * 134
   * 首页-特邀会场-授权手机号入会/绑定结果
   * 当在特邀会场授权手机号后，返回绑定/入会结果
   */
  targetSalesJoinVipResult(options : RegisterOptions) {
    const { success, successType, failReason } = options;
    this.track(
      "c_ebmp_target_sales_join_vip_result",
      {
        c_success: success ? "是" : "否",
        c_success_type: successType || "",
        c_fail_reason: failReason || "",
      },
      "首页-特邀会场-授权手机号入会/绑定结果"
    );
  }

  /**
   * 首页-特邀会场-点击即刻参加
   * 特邀会场授权手机号绑定会员/入会成功后点击弹窗上的即刻参加按钮
   */
  targetSalesParticipateNow(popupType : string) {
    this.track(
      "c_ebmp_target_sales_participate_now",
      {
        c_popup_type: popupType, // 成功入会/成功绑定
      },
      "首页-特邀会场-点击即刻参加"
    );
  }
}

const registerTracker = new RegisterTracker();

export default registerTracker;
